﻿using System;

namespace b4
{
    public abstract class IR
    {
        //返回面积
        public abstract double showS();
        //检查合法性
        public abstract bool IsRight();
    }


    class rectangle : IR
    {
        public rectangle(double a,double b)
        {
            length = a;
            width = b;
        }

        private double length;
        private double width;

        public override double showS()
        {
            if (!IsRight())
            {
                Console.WriteLine("此长方形为非法图形");
                return 0;
            }
            return length * width;
        }
        public override bool IsRight()
        {
            if ((length <= 0) || (width <= 0))
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
    class square :IR
    {
        public square(double a)
        {
            side = a;
        }
        private double side;
        public override double showS()
        {
            if (!IsRight())
            {
                Console.WriteLine("此正方形为非法图形");
                return 0;
            }
            return side*side;
        }
        public override bool IsRight()
        {
            if (side <= 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


    }
    class triangle : IR
    {
        public triangle(double a,double b,double c)
        {
            length1 = a;
            length2 = b;
            length3 = c;
        }

        private double length1;
        private double length2;
        private double length3;

        public override bool IsRight()
        {
            if ((length1 <= 0) || (length2 <= 0) || (length3 <= 0))
            {
                return false;
            }else if ((length1 + length2 <= length3) || (length2 + length3 <= length1) || (length1 + length3 <= length2))
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public override double showS()
        {
            if (!IsRight())
            {
                Console.WriteLine("此三角形为非法图形");
                return 0;
            }
            return Math.Sqrt((length1 + length2 + length3) * (length1 + length2 - length3)
                * (length1 + length3 - length2) * (length2 + length3 - length1)) / 4;
        }
    }





    class Program
    {
        static void Main(string[] args)
        {
            rectangle a = new rectangle(2, 1);
            Console.WriteLine("长方形面积为：" + a.showS());

            square b = new square(4);
            Console.WriteLine("正方形面积为：" + b.showS());

            triangle c = new triangle(1, 2, 3);
            Console.WriteLine("三角形面积为：" + c.showS());
            
        }
    }
}
